//
//  XVTextField.m
//  整合
//
//  Created by  刘尚威 on 2017/3/3.
//  Copyright © 2017年  刘尚威. All rights reserved.
//

#import "XVTextField.h"


@implementation XVTextField

+ (UITextField *)TextFieldWithText:(NSString *)text
                       placeholder:(NSString *)placeholder
                              font:(CGFloat)font
                         textColor:(UIColor *)textColor
                   secureTextEntry:(BOOL)secureTextEntry
                      keyboardType:(UIKeyboardType)keyboardType
                         addToView:(UIView *)view
                             frame:(CGRect)frame
{
    UITextField *newTextField = [[UITextField alloc] initWithFrame:frame];
    newTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    
    //字符串垂直居中
    newTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    
    /*字符串不能为空，不然某些特定的判断会出错*/
    //当前输入字符串
    if (text == nil) {
        newTextField.text = @"";
    }
    else {
        newTextField.text = text;
    }
    
    //默认背景字符串
    if (placeholder == nil) {
        newTextField.placeholder = @"";
    }
    else {
        newTextField.placeholder = placeholder;
    }
    
    //字体大小
    newTextField.font = [UIFont systemFontOfSize:font];
    
    //字体颜色
    if (textColor == nil) {
        newTextField.textColor = [UIColor blackColor];
    }else{
        newTextField.textColor = textColor;
    }
    
    //输入的字符串是否为密码的格式
    newTextField.secureTextEntry = secureTextEntry;
    
    //键盘设置
    newTextField.keyboardType = keyboardType;
    
    if (view != nil) {
        [view addSubview:newTextField];
    }
    
    return newTextField;
}

- (void)shake {
    CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];
    keyFrame.duration = 0.3;
    CGFloat x = self.layer.position.x;
    keyFrame.values = @[@(x + 30), @(x - 30), @(x + 20), @(x - 20), @(x + 10), @(x - 10), @(x + 5), @(x - 5)];
    [self.layer addAnimation:keyFrame forKey:@"shake"];
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
